﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MotionControl.Modules.LightSource.LightControl
{
    internal class JCKwLight : IJFLight
    {
        string ErrorInfo = null;
        string sCom = null;
        int ChanelNum = 0;
        string sType = "KV";

        RS232Library sPort = null;
        public JCKwLight(string _scom, int _chanelnum)
        {
            sCom = _scom;
            ChanelNum = _chanelnum;
            sPort = new RS232Library(_scom);
        }
        public string GetErrorInfo()
        {
            return ErrorInfo;
        }

        bool bConnect = false;
        public bool InitLight()
        {
            try
            {
                if(!sPort.OpenPort())
                {
                    ErrorInfo = "连接光源控制器失败！";
                    return false;
                }

                bConnect = true;
                return true;
            }
            catch
            {
               
                ErrorInfo = "连接光源控制器失败！";
                return false;
            }
        }

        public bool GetIntensity(int channel, out int lightVal)
        {
            try
            {

                Thread.Sleep(100);

                lightVal = 0;
                string headStr = "#";
                string result = sPort.WriteCommand(headStr + "4" + channel + "000");
                if (result.Contains("4" + channel + "0"))
                {
                    string[] numL = result.Split(new string[] { "4" + channel + "0" }, StringSplitOptions.None);
                    string NUM = numL[1].Substring(0, 2);
                    
                    lightVal = Convert.ToInt32(NUM, 16);
                }
                else
                {
                   
                    result = sPort.WriteCommand(headStr + "4" + channel + "000");
                    if (result.Contains("4" + channel + "0"))
                    {
                        string[] numL = result.Split(new string[] { "4" + channel + "0" }, StringSplitOptions.None);
                        string NUM = numL[1].Substring(0, 2);
                        
                        lightVal = Convert.ToInt32(NUM, 16);
                    }
                    else
                    {
                        lightVal = 0;
                    }
                }


                return true;
            }

            catch 
            {
                lightVal = 0;
                ErrorInfo = "获取亮度值失败！";
                return false; 
            }           
        }
        
        public bool SetIntensity(int channel, int lightValue)
        {
            try
            {                
                string light = "";
                if (lightValue < 16)
                {
                    light = "0" + lightValue.ToString("X");
                }
                else { light = lightValue.ToString("X"); }

                string result = sPort.WriteCommand("#3" + channel + "0" + light);
                if (result.Contains("&"))
                {
                    ErrorInfo = "设定光源失败！";
                    return false;
                }
                return true;
            }
            catch
            {
                lightValue = 0;
                ErrorInfo = "设定亮度值失败！";
                return false;
            }
        }

        public bool TurnOffChannel(int num)
        {
            string light = "";
   
            light = "0" + 0.ToString("X");          

            string result = sPort.WriteCommand("#3" + num + "0" + light);
            if (result.Contains("&"))
            {
                ErrorInfo = "关闭光源失败！";
                return false;
            }        
            return true;
        }

        public bool TurnOnChannel(int num)
        {
            return true;
        }
        public bool DisConnect()
        {
            try
            {
                sPort.ClosePort();
                return true;
            }
            catch (Exception e)
            {
                ErrorInfo = "关闭设备失败";
                return false;
            }


            return false;
        }

        public string type()
        {
            return sType;
        }

        /// <summary>
        /// 当前光源com口
        /// </summary>
        /// <returns></returns>
        public string comIndex()
        {
            return sCom;
        }

        /// <summary>
        /// 当前光源的通道数
        /// </summary>
        /// <returns></returns>
        public int channels()
        {
            return ChanelNum;
        }
    }
}
